var ContactsValidation = function () {

    var handleValidation1 = function() {
        // for more info visit the official plugin documentation: 
            // http://docs.jquery.com/Plugins/Validation

            var form1 = $('#form_contacts');
            var error1 = $('.alert-danger', form1);
            var success1 = $('.alert-success', form1);

            form1.validate({
                errorElement: 'span', //default input error message container
                errorClass: 'help-block', // default input error message class
                focusInvalid: false, // do not focus the last invalid input
                ignore: "",
                rules: {
                	customer_id:{
                    	required: true
                    },
                	name:{
                    	required: true
                    },
                    gender:{
                    	required: true
                    },
                    telephone:{
                    	required: true,
                    	number:true,
                    	minlength:11
                    },
                    phone:{
                    	number:true,
                    	minlength:11
                    },
                    qq:{
                    	required: true,
                    	number:true
                    },
                    fax:{
                    	number:true,
                    	minlength:11
                    },
                    email:{
                    	email:true
                    }
                },

                invalidHandler: function (event, validator) { //display error alert on form submit              
                    success1.hide();
                    error1.show();
                    App.scrollTo(error1, -200);
                },

                highlight: function (element) { // hightlight error inputs
                    $(element)
                        .closest('.form-group').addClass('has-error'); // set error class to the control group
                },

                unhighlight: function (element) { // revert the change done by hightlight
                    $(element)
                        .closest('.form-group').removeClass('has-error'); // set error class to the control group
                },

                success: function (label) {
                    label
                        .closest('.form-group').removeClass('has-error'); // set success class to the control group
                    
                },

                submitHandler: function (form) {
                	$.ajax({
        	            type:"post",
        	            url:"/crm/admin/contacts!save.action",
        	            dataType:"json",
        	            data:"contacts.name="+$("input[name='name']").val()+"&customerId="+$("#customer_id").val()+
        	            	"&contacts.gender="+$("#gender").val()+"&contacts.telephone="+$("input[name='telephone']").val()+
        	            	"&contacts.phone="+$("input[name='phone']").val()+"&contacts.fax="+$("input[name='fax']").val()+
        	            	"&contacts.email="+$("input[name='email']").val()+"&contacts.zipcode="+$("input[name='zipcode']").val()+
        	            	"&contacts.address="+$("input[name='address']").val()+"&contacts.type="+$("input[name='type']:checked").val()+
        	            	"&contacts.remarks="+$("input[name='remarks']").val()+"&contacts.qq="+$("input[name='qq']").val(),
        	            cache:false,
        	            beforeSend:function(XMLHttpRequest){
        	            },
        	            success:function(json){
        	            	result = json.result;
        	            	if(result){
        	            		if(confirm("提示!更新成功。是否返回联系人列表？")==true){
        	            			window.location.href="/crm/admin/contacts!list.action";
        	            		}else{
        	            			
        	            		}
        	            	}else{
        	            		alert("程序异常,请联系开发人员!");
        	            	}
        	                
        	            },
        	           	complete:function(XMLHttpRequest,textStatus){
        	           	},
        	          	error:function(XMLHttpRequest,textStatus,errorThrown){
        	                   alert("网络错误,重稍后重试!");
        	          	}
                    });
//                    success1.show();
                    error1.hide();
                }
            });


    }

    var handleWysihtml5 = function() {
        if (!jQuery().wysihtml5) {
            
            return;
        }

        if ($('.wysihtml5').size() > 0) {
            $('.wysihtml5').wysihtml5({
                "stylesheets": ["assets/plugins/bootstrap-wysihtml5/wysiwyg-color.css"]
            });
        }
    }

    return {
        //main function to initiate the module
        init: function () {

            handleWysihtml5();
            handleValidation1();

        }

    };

}();